#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"
#include "include/libfetion.h"

//fuck the struct of DLIST



MODULE = Fetion		PACKAGE = Fetion		

bool
fx_init()
    CODE:
    RETVAL=fx_init();
    OUTPUT:RETVAL

void
fx_terminate()
    CODE:
    fx_terminate();


bool
fs_login(char * uid, char * password)
    CODE:
    RETVAL=fs_login(uid, password);
    OUTPUT:RETVAL   


void
fx_loginout()
    CODE:
    fx_loginout();

bool
fs_send_sms(long who, char *message)
    CODE:
    RETVAL=fs_send_sms(who, message);
    OUTPUT:RETVAL   


bool
fs_send_sms_by_mobile_no(char *mobile_no, char *message)
    CODE:
    RETVAL=fs_send_sms_by_mobile_no(mobile_no, message);
    OUTPUT:RETVAL   


bool
fs_send_sms_to_self(char *message)
    CODE:
    RETVAL=fs_send_sms_to_self(message);
    OUTPUT:RETVAL   

SV* 
fx_get_account()
    INIT:
    AV * results;
    CODE:
    Fetion_Account * account = NULL;
    DList* list =  NULL;
    results = (AV *)sv_2mortal((SV *)newAV());
    list = (DList*)fx_get_account();
    while(list) {
        account =(Fetion_Account *)list->data;
        if (account) {     
            HV *hash = (HV *)sv_2mortal((SV *)newHV());
            hv_store(hash , "id", 2, newSVuv(account->id), 0);
            hv_store(hash , "uri", 3, newSVpv(account->uri, 0), 0);
            if (account->local_name != NULL) {
                hv_store(hash , "local_name", 10, newSVpv(account->local_name, 0), 0);  
            }
            if (account->buddy_lists != NULL) {
                hv_store(hash , "buddy_lists", 11, newSVpv(account->buddy_lists, 0), 0);
            }

            hv_store(hash , "online_status", 13, newSViv(account->online_status), 0);
            if (account->personal != NULL) {
                Fetion_Personal* personal = account->personal;
                HV *hash_personal = (HV *)sv_2mortal((SV *)newHV());                
                if (personal->nickname != NULL) {
                    hv_store(hash_personal , "nickname", 8, newSVpv(personal->nickname, 0), 0);
                }


                hv_store(hash , "personal", 8, newRV((SV*)hash_personal), 0);    
            }
             
            av_push(results,  newRV((SV *)hash));   
        }
        list = d_list_next(list); 
    }
    RETVAL=newRV((SV*)results);
    OUTPUT:RETVAL     
